package com.youku.plugins.commonui.view
{
   import flash.display.MovieClip;
   import com.youku.interfaces.IPlayerProxy;
   import com.youku.plugins.commonui.CommonUIEvent;
   import com.youku.events.PlayerControlEvent;
   import com.youku.events.ConductorEvent;
   import com.youku.events.PluginEvent;
   import com.youku.events.CoreEvent;
   import flash.events.KeyboardEvent;
   import com.youku.PlayerConfig;
   import com.youku.events.ReportEvent;
   import com.youku.data.LoadTimeData;
   import com.youku.conductor.ConductorState;
   import com.youku.utils.ReportUtil;
   import flash.utils.Timer;
   import flash.events.TimerEvent;
   import flash.ui.Keyboard;
   
   public class VideoOverLayerMediator extends Object
   {
      
      public function VideoOverLayerMediator(param1:MovieClip, param2:IPlayerProxy) {
         super();
         this._mainMC = param1;
         this._playerProxy = param2;
         this._layer = new VideoOverLayer();
         this._keyTimer = new Timer(1 * 1000);
         this._keyTimer.addEventListener(TimerEvent.TIMER,this.onTimer);
         this.init();
      }
      
      private var _mainMC:MovieClip;
      
      private var _playerProxy:IPlayerProxy;
      
      private var _layer:VideoOverLayer;
      
      private var _isPlaying:Boolean = true;
      
      private var _sendBuffer:Boolean = false;
      
      private function init() : void {
         this._layer.addEventListener(CommonUIEvent.VIDEO_LAYER_CLICK,this.onLayerEvent);
         this._layer.addEventListener(CommonUIEvent.VIDEO_LAYER_DOUBLECLICK,this.onLayerEvent);
         this._layer.addEventListener(CommonUIEvent.VIDEO_LAYER_CLOSE_FULLSCREEN,this.onLayerEvent);
         this._playerProxy.addEventListener(PlayerControlEvent.FULLSCREEN_CHANGED,this.onPlayerControlEvent);
         this._playerProxy.addEventListener(PlayerControlEvent.PLAYER_SIZE_CHANGED,this.onPlayerControlEvent);
         this._playerProxy.addEventListener(ConductorEvent.STATE_CHANGED,this.onStateChanged);
         this._playerProxy.addEventListener(PluginEvent.CONTROL_BAR_HIDE,this.onOtherPluginEvent);
         this._playerProxy.addEventListener(PluginEvent.CONTROL_BAR_SHOW,this.onOtherPluginEvent);
         this._playerProxy.addEventListener(CoreEvent.PAUSE,this.onCoreEvent);
         this._playerProxy.addEventListener(CoreEvent.RESUME,this.onCoreEvent);
         this._playerProxy.addEventListener(CoreEvent.BUFFER_EMPTY,this.onCoreEvent);
         this._playerProxy.addEventListener(CoreEvent.BUFFER_FULL,this.onCoreEvent);
         this._playerProxy.addEventListener(CoreEvent.BUFFER_PERCENT,this.onCoreEvent);
         this._playerProxy.addEventListener(CoreEvent.QUALITY_CHANGED,this.onCoreEvent);
         this._playerProxy.addEventListener(KeyboardEvent.KEY_DOWN,this.onKeyDown);
         this._playerProxy.addEventListener(CoreEvent.YK_GET_DEBUG_LOCALID,this.onGetYKDebugLocalID);
      }
      
      private function onPlayerControlEvent(param1:PlayerControlEvent) : void {
         var _loc2_:* = NaN;
         var _loc3_:* = NaN;
         var _loc4_:* = false;
         var _loc5_:* = NaN;
         switch(param1.type)
         {
            case PlayerControlEvent.PLAYER_SIZE_CHANGED:
               _loc2_ = param1.data.width;
               _loc3_ = param1.data.height;
               _loc4_ = param1.data.fullscreen;
               _loc5_ = param1.data.cheight;
               if(_loc4_)
               {
                  this._layer.setSize(_loc2_,_loc3_);
               }
               else
               {
                  this._layer.setSize(_loc2_,_loc3_ - _loc5_);
               }
               break;
            case PlayerControlEvent.FULLSCREEN_CHANGED:
               _loc2_ = param1.data.width;
               _loc3_ = param1.data.height;
               _loc4_ = param1.data.fullscreen;
               _loc5_ = param1.data.cheight;
               if(_loc4_)
               {
                  this._layer.setSize(_loc2_,_loc3_);
               }
               else
               {
                  this._layer.setSize(_loc2_,_loc3_ - _loc5_);
               }
               this._layer.toggleFullScreen(_loc4_);
               break;
         }
      }
      
      private function onOtherPluginEvent(param1:PluginEvent) : void {
         switch(param1.type)
         {
            case PluginEvent.CONTROL_BAR_SHOW:
               this._layer.showCloseButton(true);
               break;
            case PluginEvent.CONTROL_BAR_HIDE:
               this._layer.showCloseButton(false);
               break;
            case PluginEvent.CONTROL_BAR_DRAG_DRAGGING:
            case PluginEvent.CONTROL_BAR_DRAG_FINISH:
            case PluginEvent.CONTROL_BAR_SEEK:
               this._layer.showSeekTime(param1.data.time,this._playerProxy.playerData.videoTotalTime);
               break;
         }
      }
      
      private function onCoreEvent(param1:CoreEvent) : void {
         var _loc2_:* = NaN;
         switch(param1.type)
         {
            case CoreEvent.RESUME:
               this._isPlaying = true;
               break;
            case CoreEvent.PAUSE:
               this._isPlaying = false;
               break;
            case CoreEvent.BUFFER_EMPTY:
               this._sendBuffer = true;
               PlayerConfig.loadtimeData.ltbuffer_start = new Date().getTime();
               this._layer.showLoading(true);
               break;
            case CoreEvent.BUFFER_FULL:
               if(this._sendBuffer)
               {
                  PlayerConfig.loadtimeData.ltbuffer_end = new Date().getTime();
                  PlayerConfig.loadtimeData.ltbuffer = PlayerConfig.loadtimeData.ltbuffer_end - PlayerConfig.loadtimeData.ltbuffer_start;
                  this._playerProxy.dispatchEvent(new ReportEvent(ReportEvent.LOAD_TIME_REPORT,
                     {
                        "isError":false,
                        "type":LoadTimeData.TYPE_BUFFER,
                        "errorCode":null
                     }));
                  this._sendBuffer = false;
               }
               this._layer.showLoading(false);
               break;
            case CoreEvent.BUFFER_PERCENT:
               _loc2_ = param1.data.per;
               this._layer.setLoadingPercent(_loc2_);
               break;
            case CoreEvent.QUALITY_CHANGED:
               break;
         }
      }
      
      private function onStateChanged(param1:ConductorEvent) : void {
         var _loc2_:String = param1.data as String;
         switch(_loc2_)
         {
            case ConductorState.VIDEO:
               this.hide();
               this.show();
               break;
            case ConductorState.INITIALIZE:
            case ConductorState.PRE_VIDEO:
            case ConductorState.POST_VIDEO:
            case ConductorState.VIDEO_END:
            case ConductorState.ERROR:
               this.hide();
               break;
            case ConductorState.AD:
               this._layer.enable = false;
               break;
         }
      }
      
      private function onLayerEvent(param1:CommonUIEvent) : void {
         switch(param1.type)
         {
            case CommonUIEvent.VIDEO_LAYER_CLICK:
               if(this._isPlaying)
               {
                  ReportUtil.sendUserActionLog(ReportUtil.PAUSE,ReportUtil.METHOD_FRAME_CLICK);
               }
               else
               {
                  ReportUtil.sendUserActionLog(ReportUtil.PLAY,ReportUtil.METHOD_FRAME_CLICK);
               }
               this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.VIDEO_LAYER_TOGGLE_PLAYPAUSE));
               break;
            case CommonUIEvent.VIDEO_LAYER_DOUBLECLICK:
               if(this._playerProxy.playerData.isInFullScreen)
               {
                  ReportUtil.sendUserActionLog(ReportUtil.QUIT_FULLSCREEN,ReportUtil.METHOD_DOUBLE_CLICK);
               }
               else
               {
                  ReportUtil.sendUserActionLog(ReportUtil.ENTER_FULLSCREEN,ReportUtil.METHOD_DOUBLE_CLICK);
               }
               this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.VIDEO_LAYER_TOGGLE_FULLSCREEN));
               break;
            case CommonUIEvent.VIDEO_LAYER_CLOSE_FULLSCREEN:
               this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.VIDEO_LAYER_CLOSE_FULLSCREEN));
               break;
         }
      }
      
      private function show() : void {
         this._mainMC.addChild(this._layer);
         this._layer.x = this._layer.y = 0;
         this._layer.enable = true;
      }
      
      private function hide() : void {
         if(this._mainMC.contains(this._layer))
         {
            this._mainMC.removeChild(this._layer);
         }
         this._layer.x = this._layer.y = 0;
         this._layer.enable = false;
      }
      
      private var _pKeyClickCounts:Number = 0;
      
      private var _keyTimer:Timer;
      
      private var _localConnectID:String = "";
      
      private function onTimer(param1:TimerEvent) : void {
         this._keyTimer.stop();
         this._pKeyClickCounts = 0;
      }
      
      private function onGetYKDebugLocalID(param1:CoreEvent) : void {
         this._localConnectID = param1.data.name;
      }
      
      private function onKeyDown(param1:KeyboardEvent) : void {
         switch(param1.keyCode)
         {
            case Keyboard.P:
               this._pKeyClickCounts++;
               if(this._pKeyClickCounts == 3)
               {
                  this._pKeyClickCounts = 0;
                  this._layer.showLocalConnectID(this._localConnectID);
               }
               this._keyTimer.reset();
               this._keyTimer.start();
               break;
         }
      }
   }
}
